package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.Admin;
import com.itheima.mapper.AdminMapper;
import com.itheima.service.IAdminService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Autowired
    AdminMapper adminMapper;

    @Override
    public Admin findByUsername(String username) {

        LambdaQueryWrapper<Admin> qw=new LambdaQueryWrapper<>();
        qw.eq(Admin::getUsername,username);

        return  adminMapper.selectOne(qw);
    }

    @Override
    public PageBeanVo findByPageAndKeyword(Integer pageNum, Integer pageSize, String keyword) {

        LambdaQueryWrapper<Admin> qw=new LambdaQueryWrapper<>();
        //匹配关键字
        if(keyword!=null){
            qw.like(Admin::getUsername,keyword).or().like(Admin::getNickName,keyword);
        }
        //添加分页条件
        Page<Admin> page=new Page<>(pageNum,pageSize);

        adminMapper.selectPage(page,qw);

        //封装对象
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }

    @Override
    public Boolean saveAdmin(Admin admin) {
        //查询此用户名是否已经存在
        String username = admin.getUsername();
        LambdaQueryWrapper<Admin> qw=new LambdaQueryWrapper<>();
        qw.eq(Admin::getUsername,username);
        Admin admin2 = adminMapper.selectOne(qw);
        if(admin2!=null){
            return false;
        }
        //获得密码
        String password=admin.getPassword();
        if(StrUtil.isEmpty(password)){
            //若没设置密码，赋予默认密码
            password="admin";
        }
        //将密码进行md5加密
        String pwd=SecureUtil.md5(password);
        System.out.println(pwd);
        admin.setPassword(pwd);
        adminMapper.insert(admin);
        return true;
    }

    @Override
    public void updateLoginTime(Admin one) {
        adminMapper.updateById(one);
    }

    @Override
    public void deleteById(Long id) {
        adminMapper.deleteById(id);
    }

    @Override
    public void updateAdmin(Admin admin) {
        admin.setPassword(SecureUtil.md5(admin.getPassword()));
        adminMapper.updateById(admin);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Admin admin=new Admin();
        admin.setId(id);
        admin.setStatus(status);
        adminMapper.updateById(admin);
    }

    @Override
    public Admin findById(Long id) {
        return adminMapper.selectById(id);
    }
}
